package utils

import (
	"encoding/json"
	"net/http"
)

/// 返回Json
func HandleJsonResponse(w http.ResponseWriter, code string, data interface{}) {
	response := map[string]interface{}{
		"code": code,
		"msg":  RecodeText(code),
	}
	if data != nil {
		response["data"] = data
	}

	w.Header().Set("Content-Type", "application/json")
	if err := json.NewEncoder(w).Encode(response); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
}
